import domain.Fruits;
import domain.ShoppingCart;
import domain.ShoppingItem;
import service.impl.DiscountPromotion;
import service.impl.FullReduction;

import java.math.BigDecimal;

public class PriceCalculation {
    // 定义各种水果
    private static final Fruits APPLE = new Fruits("苹果", new BigDecimal("8"));
    private static final Fruits STRAWBERRY = new Fruits("草莓", new BigDecimal("13"));
    private static final Fruits MANGO = new Fruits("芒果", new BigDecimal("20"));
    public static void main(String[] args) {
        //题目一
        ShoppingCart cartA = new ShoppingCart(2);
        // 购买5斤苹果和3斤草莓
        cartA.addItem(new ShoppingItem(APPLE, 5));
        cartA.addItem(new ShoppingItem(STRAWBERRY, 3));
        BigDecimal totalPriceA = cartA.calculateTotalPrice();
        System.out.println("购买5斤苹果和3斤草莓的总价：" + totalPriceA + "元");
        // 验证计算结果
        BigDecimal expectedPriceA = APPLE.getPrice()
                .multiply(new BigDecimal(5))
                .add(STRAWBERRY.getPrice().multiply(new BigDecimal(3)));
        verifyResult(totalPriceA, expectedPriceA, "题目一");

        //题目二
        ShoppingCart cartB = new ShoppingCart(3);
        // 购买4斤苹果、2斤草莓和1斤芒果
        cartB.addItem(new ShoppingItem(APPLE, 4));
        cartB.addItem(new ShoppingItem(STRAWBERRY, 2));
        cartB.addItem(new ShoppingItem(MANGO, 1));
        BigDecimal totalPriceB = cartB.calculateTotalPrice();
        System.out.println("购买4斤苹果、2斤草莓和1斤芒果的总价：" + totalPriceB + "元");
        // 验证计算结果
        BigDecimal expectedPriceB = APPLE.getPrice()
                .multiply(new BigDecimal(4))
                .add(STRAWBERRY.getPrice().multiply(new BigDecimal(2)))
                .add(MANGO.getPrice());
        verifyResult(totalPriceB, expectedPriceB, "题目二");

        //题目三
        ShoppingCart cartC = new ShoppingCart(3);
        // 设置草莓打折策略
        cartC.setPromotionCalculate(new DiscountPromotion());
        // 购买3斤苹果、5斤草莓和2斤芒果
        cartC.addItem(new ShoppingItem(APPLE, 3));
        cartC.addItem(new ShoppingItem(STRAWBERRY, 5));
        cartC.addItem(new ShoppingItem(MANGO, 2));
        BigDecimal totalPriceC = cartC.calculateTotalPrice();
        System.out.println("购买3斤苹果、5斤草莓(8折)和2斤芒果的总价：" + totalPriceC + "元");
        // 验证计算结果
        BigDecimal applePriceC = APPLE.getPrice().multiply(new BigDecimal(3));
        BigDecimal strawberryPriceC = STRAWBERRY.getPrice()
                .multiply(new BigDecimal(5))
                .multiply(new BigDecimal("0.8"));
        BigDecimal mangoPriceC = MANGO.getPrice().multiply(new BigDecimal(2));
        BigDecimal expectedPriceC = applePriceC.add(strawberryPriceC).add(mangoPriceC);
        verifyResult(totalPriceC, expectedPriceC, "题目三");

        //题目四
        ShoppingCart cartD = new ShoppingCart(3);
        // 设置满减策略
        cartD.setPromotionCalculate(new FullReduction());
        // 购买6斤苹果、4斤草莓和3斤芒果，总价超过100
        cartD.addItem(new ShoppingItem(APPLE, 6));
        cartD.addItem(new ShoppingItem(STRAWBERRY, 4));
        cartD.addItem(new ShoppingItem(MANGO, 3));
        BigDecimal totalPriceD = cartD.calculateTotalPrice();
        System.out.println("购买6斤苹果、4斤草莓和3斤芒果(满100减10)的总价：" + totalPriceD + "元");
        //验证计算结果
        BigDecimal applePriceD = APPLE.getPrice().multiply(new BigDecimal(6));
        BigDecimal strawberryPriceD = STRAWBERRY.getPrice().multiply(new BigDecimal(4));
        BigDecimal mangoPriceD = MANGO.getPrice().multiply(new BigDecimal(3));
        BigDecimal originalPriceD = applePriceD.add(strawberryPriceD).add(mangoPriceD);
        BigDecimal expectedPriceD = originalPriceD.compareTo(new BigDecimal("100")) >= 0
                ? originalPriceD.subtract(new BigDecimal("10"))
                : originalPriceD;
        verifyResult(totalPriceD, expectedPriceD, "题目四");
    }

    /**
     * 验证计算结果是否正确
     */
    private static void verifyResult(BigDecimal actual, BigDecimal expected, String scenario) {
        if (actual.compareTo(expected) == 0) {
            System.out.println(scenario + "计算结果正确！");
        } else {
            System.out.println(scenario + "计算结果错误！");
            System.out.println("预期结果：" + expected + "元");
            System.out.println("实际结果：" + actual + "元");
        }
    }
}